﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ANZ_AIM_BLL.Model;
using ANZ_AIM_GUI.Utils;
using System.IO;
using System.Reflection;
using ANZ_AIM_BLL;
using ANZ_AIM_FRAMEWORK;
using log4net;

namespace ANZ_AIM_OFFLINETOOL
{
    public partial class frmEmployeeOffline : Form
    {
        //log4net
        private static readonly ILog logger = LogManager.GetLogger(typeof(frmEmployeeOffline));

       // public EmploymentOffline employmentTemp { get; set; }

        public DataTable dtEmploymentChange { get; set; }

        public frmEmployeeOffline()
        {
            InitializeComponent();
        }

        public frmEmployeeOffline(ref DataTable dtEmploymentChange)
            : this()
        {
            try
            {
                dgvData.DataSource = null;
                dgvData.Columns.Clear();
                dgvData.DataSource = dtEmploymentChange;
                dgvData.AutoResizeColumns();

                dgvData.Columns[0].HeaderText = "Profile ID";
                dgvData.Columns[1].HeaderText = "Batch File";
                dgvData.Columns[2].HeaderText = "Field Change";
                dgvData.Columns[3].HeaderText = "Old Value";
                dgvData.Columns[4].HeaderText = "New Value";
                dgvData.Columns[5].Visible = false;

                this.dtEmploymentChange = dtEmploymentChange;

                //this.employmentTemp = new EmploymentOffline();
            }
            catch (Exception ex) { throw ex; }
        }

        #region Action
        private void btnCloseChange_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void btnUpdateEmployment_Click(object sender, EventArgs e)
        {
            try
            {
                int selectedRowCount = dgvData.Rows.GetRowCount(DataGridViewElementStates.Selected);
                if (selectedRowCount > 0)
                {
                    if (string.IsNullOrEmpty(txtNewValue.Text.Trim()))
                    {
                        MessageBox.Show("New Value cannot be empty.", Constant.SYS_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    //edit invidual
                    if (!string.IsNullOrEmpty(txtProfileIDChange.Text.Trim()))
                    {
                        DataRow[] foundRows = dtEmploymentChange.Select("ProfileID= '" + txtProfileIDChange.Text + "' AND FieldChange = '" + txtField.Text + "'");
                        if (foundRows.Length > 0)
                        {
                            foundRows[0]["NewValue"] = txtNewValue.Text;
                        }
                    }

                    //edit batch file
                    if (!string.IsNullOrEmpty(txtBatchFile.Text.Trim()))
                    {
                        DataRow[] foundRows = dtEmploymentChange.Select("BatchFile= '" + txtBatchFile.Text + "' AND FieldChange = '" + txtField.Text + "'");
                        if (foundRows.Length > 0)
                        {
                            foundRows[0]["NewValue"] = txtNewValue.Text;
                        }
                    }
                }
                else
                {
                    MessageBox.Show("Not employment is selected.", Constant.SYS_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.InnerException == null ? ex.Message : ex.InnerException.Message, Constant.SYS_ERROR, MessageBoxButtons.OK);
                logger.Error(ex.Message + " - " + ex.StackTrace);
            }
        }
        private void dgvData_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            try
            {
                int selectedRowCount = dgvData.Rows.GetRowCount(DataGridViewElementStates.Selected);
                if (selectedRowCount > 0)
                {
                    DataRow dr = ((DataRowView)dgvData.SelectedRows[0].DataBoundItem).Row;
                    txtProfileIDChange.Text = dr["ProfileID"].ToString();
                    txtBatchFile.Text = dr["BatchFile"].ToString();
                    txtField.Text = dr["FieldChange"].ToString();
                    txtOldValue.Text = dr["OldValue"].ToString();
                    txtNewValue.Text = dr["NewValue"].ToString();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.InnerException == null ? ex.Message : ex.InnerException.Message, Constant.SYS_ERROR, MessageBoxButtons.OK);
                logger.Error(ex.Message + " - " + ex.StackTrace);
            }
        }
        private void btnDeleteChange_Click(object sender, EventArgs e)
        {
            try
            {
                int selectedRowCount = dgvData.Rows.GetRowCount(DataGridViewElementStates.Selected);
                if (selectedRowCount > 0)
                {
                    DialogResult result = MessageBox.Show("Are you sure to delete this change?", "Delete confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question,
                       MessageBoxDefaultButton.Button2);
                    if (result == DialogResult.Yes)
                    {
                        DataRow dr = ((DataRowView)dgvData.SelectedRows[0].DataBoundItem).Row;
                        string profileID = dr["ProfileID"].ToString();
                        string fieldChange = dr["FieldChange"].ToString();
                        string batchFile = dr["BatchFile"].ToString();

                        //delete invidual change
                        if (!string.IsNullOrEmpty(profileID))
                        {

                            DataRow[] foundRows = dtEmploymentChange.Select("ProfileID= '" + profileID + "' AND FieldChange = '" + fieldChange + "'");
                            string firstValue = "";
                            if (foundRows.Length > 0)
                            {
                                firstValue = foundRows[0]["FirstValue"].ToString();
                                dtEmploymentChange.Rows.Remove(foundRows[0]);
                            }
                        }

                        //delete batch file change
                        if (!string.IsNullOrEmpty(batchFile))
                        {

                            DataRow[] foundRows = dtEmploymentChange.Select("BatchFile= '" + batchFile + "' AND FieldChange = '" + fieldChange + "'");
                            string firstValue = "";
                            if (foundRows.Length > 0)
                            {
                                firstValue = foundRows[0]["FirstValue"].ToString();
                                dtEmploymentChange.Rows.Remove(foundRows[0]);
                            }
                        }

                        //clear data
                        ClearData();
                    }
                }
                else
                {
                    MessageBox.Show("Not employment is selected.", Constant.SYS_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.InnerException == null ? ex.Message : ex.InnerException.Message, Constant.SYS_ERROR, MessageBoxButtons.OK);
                logger.Error(ex.Message + " - " + ex.StackTrace);
            }
        }

        #endregion

        private void ClearData()
        {
            txtProfileIDChange.Text = "";
            txtBatchFile.Text = "";
            txtField.Text = "";
            txtNewValue.Text = "";
            txtOldValue.Text = "";
        }
    }
}
